// zlibSampleCPP.cpp : Defines the entry point for the console application.
//

#include <windows.h>
#include "compressor.h"

#define SOURCE_BUFFER_SIZE (0x10000)

int _tmain(int argc, _TCHAR* argv[])
{
	RETAILMSG(TRUE, (_T("Generating %i bytes of data..."), SOURCE_BUFFER_SIZE));

	// create a source buffer
	BYTE *source = (BYTE*)LocalAlloc(LPTR, SOURCE_BUFFER_SIZE);

	// the compress algorithm looks for repeat sequences, so generate data that has that
	srand(GetTickCount());	
	for(int i = 0 ; i < SOURCE_BUFFER_SIZE ; i+=4)
	{
		BYTE val = (rand() % 0x100);
		source[i] = val;
		source[i+1] = val;
		source[i+2] = val;
		source[i+3] = val;
	}

	RETAILMSG(TRUE, (_T("ok\r\n")));

	// determine max compressed size
	BYTE *compressed = NULL;
	DWORD size = 0;
	Compressor::CompressData(source, SOURCE_BUFFER_SIZE, compressed, &size);

	RETAILMSG(TRUE, (_T("Max compressed size: %i\r\n"), size));

	// create the compressed buffer
	compressed = (BYTE*)LocalAlloc(LPTR, size);

	RETAILMSG(TRUE, (_T("Compressing...")));

	DWORD et = GetTickCount();
	
	// compress it
	Compressor::CompressData(source, SOURCE_BUFFER_SIZE, compressed, &size);
	
	// get ET
	et = GetTickCount() - et;

	RETAILMSG(TRUE, (_T("ok. Compression took %ims\r\n"), et));
	RETAILMSG(TRUE, (_T("Actual compressed size: %i\r\n"), size));

	// uncompress it
	RETAILMSG(TRUE, (_T("Decompressing...")));

	// wipe the original source
	ZeroMemory(source, SOURCE_BUFFER_SIZE);

	size = SOURCE_BUFFER_SIZE;

	et = GetTickCount();
	Compressor::DecompressData(compressed, size, source, &size);

	// get ET
	et = GetTickCount() - et;

	RETAILMSG(TRUE, (_T("ok. Decompression took %ims\r\n"), et));

	return 0;
}


